package com.cn.xiaonuo.tcrfinance.sysmgmt.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cn.xiaonuo.common.entity.SysLog;
import com.cn.xiaonuo.core.annotion.BusinessLog;
import com.cn.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.MenuRoleHpcInfo;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysMenuEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.enums.SystemEnum;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysLoginHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysMenuParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.ShiroHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysMenuHpcService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
/**
 * 系统菜单
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class SysMenuHpcController  {
	@Autowired
	private SysMenuHpcService sysMenuService;
	@Autowired
	private ShiroHpcService shiroService;

	/**
	 * 导航菜单
	 */
	@PostMapping("/api/sys/menu/nav")
	@ApiOperation(value = "导航菜单", notes = "导航菜单")
	public ResponseData nav(@RequestBody SysLoginHpcParam sysLoginParam){
		//List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		//Set<String> permissions = shiroService.getUserPermissions(getUserId());
		long userId=sysLoginParam.getUserId();
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(userId);
		Set<String> permissions = shiroService.getUserPermissions(userId);
		MenuRoleHpcInfo menuRoleInfo=new MenuRoleHpcInfo();
		menuRoleInfo.setMenuList(menuList);
		menuRoleInfo.setPermissions(permissions);
		//return R.ok().put("menuList", menuList).put("permissions", permissions);
		return new SuccessResponseData(menuRoleInfo);
	}

	/**
	 * 所有菜单列表
	 */
	@PostMapping("/api/sys/menu/list")
	@ApiOperation(value = "菜单列表", notes = "菜单列表")
	public List<SysMenuEntity> list(SysMenuParam sysMenuParam){
		List<SysMenuEntity> menuList = sysMenuService.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			//SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
			for(SysMenuEntity parentMenuEntity : menuList){//从内存中循环菜单列表
				if(sysMenuEntity.getParentId()==parentMenuEntity.getMenuId()){
					if(parentMenuEntity != null){
						sysMenuEntity.setParentName(parentMenuEntity.getName());
					}
				}
			}

		}
		return menuList;
	}

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@PostMapping("/api/sys/menu/select")
	@ApiOperation(value = "选择菜单(添加、修改菜单)", notes = "选择菜单(添加、修改菜单)")
	public ResponseData select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();

		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		MenuRoleHpcInfo menuRoleInfo=new MenuRoleHpcInfo();
		menuRoleInfo.setMenuList(menuList);
		//return R.ok().put("menuList", menuList);
		return new SuccessResponseData(menuRoleInfo);
	}

	/**
	 * 菜单信息
	 */
	@PostMapping("/api/sys/menu/info")
	@ApiOperation(value = "菜单信息", notes = "菜单信息")
	public ResponseData info(@RequestBody SysMenuParam sysMenuParam ){
		//SysMenuEntity menu = sysMenuService.getById(menuId);
		//return R.ok().put("menu", menu);
		return new SuccessResponseData( sysMenuService.getById(sysMenuParam.getMenuId()));
	}

	/**
	 * 保存
	 */
	@SysLog("新增菜单")
	@BusinessLog(title = "新增菜单", opType = LogAnnotionOpTypeEnum.UPDATE)
	@PostMapping("/api/sys/menu/save")
	@ApiOperation(value = "新增菜单", notes = "新增菜单")
	public ResponseData save(@RequestBody  SysMenuParam sysMenuParam){
		//数据校验
		verifyForm(sysMenuParam);
		SysMenuEntity menu=new SysMenuEntity();
		BeanUtil.copyProperties(sysMenuParam,menu);//赋值
		sysMenuService.save(menu);

		return  new SuccessResponseData();
	}

	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@BusinessLog(title = "修改菜单", opType = LogAnnotionOpTypeEnum.UPDATE)
	@PostMapping("/api/sys/menu/update")
	@ApiOperation(value = "修改菜单", notes = "修改菜单")
	public ResponseData update(@RequestBody SysMenuParam sysMenuParam){
		//数据校验
		verifyForm(sysMenuParam);
		SysMenuEntity menu=new SysMenuEntity();
		BeanUtil.copyProperties(sysMenuParam,menu);//赋值
		sysMenuService.updateById(menu);

		return  new SuccessResponseData();
	}

	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@BusinessLog(title = "删除菜单", opType = LogAnnotionOpTypeEnum.DELETE)
	@PostMapping("/api/sys/menu/delete")
	@ApiOperation(value = "删除菜单", notes = "删除菜单")
	public ResponseData delete(@RequestBody  SysMenuParam sysMenuParam){
		SuccessResponseData responseData=new SuccessResponseData();
		long menuId=sysMenuParam.getMenuId();
		if(menuId <= 31){
			responseData.setSuccess(false);
			responseData.setMessage("系统菜单，不能删除");
			return responseData;
		}

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			responseData.setSuccess(false);
			responseData.setMessage("请先删除子菜单或按钮");
			return responseData;
		}

		sysMenuService.delete(menuId);

		return responseData;
	}

	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuParam menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new ServiceException(SystemEnum.VALIDATE_MENU_INFO);
			//throw new RRException("菜单名称不能为空");
		}

		if(menu.getParentId() == null){
			throw new ServiceException(SystemEnum.VALIDATE_UP_MENU);
			//throw new RRException("上级菜单不能为空");
		}

		//菜单
		if(menu.getType() == SystemEnum.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new ServiceException(SystemEnum.VALIDATE_MENU_URL);
				//throw new RRException("菜单URL不能为空");
			}
		}

		//上级菜单类型
		int parentType = SystemEnum.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}

		//目录、菜单
		if(menu.getType() == SystemEnum.MenuType.CATALOG.getValue() ||
				menu.getType() == SystemEnum.MenuType.MENU.getValue()){
			if(parentType != SystemEnum.MenuType.CATALOG.getValue()){
				throw new ServiceException(SystemEnum.VALIDATE_MULU_TYPE);
				//throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}

		//按钮
		if(menu.getType() == SystemEnum.MenuType.BUTTON.getValue()){
			if(parentType != SystemEnum.MenuType.MENU.getValue()){
				throw new ServiceException(SystemEnum.VALIDATE_MENU_TYPE);
				//throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
